package com.authsphere.plugin.base;

import com.authsphere.common.entity.PluginData;
import com.authsphere.plugin.api.AuthSphereAfterPlugin;
import com.authsphere.plugin.api.AuthSphereAfterPluginChain;
import com.authsphere.plugin.api.WebExchange;
import com.authsphere.plugin.base.cache.BaseDataCache;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @program: AuthSphere
 * @description:
 * @author: YuKai Fan
 * @create: 2025/3/25 11:25
 **/
public abstract class AbstractAuthSphereAfterPlugin implements AuthSphereAfterPlugin {
    private static final Logger log = LoggerFactory.getLogger(AbstractAuthSphereAfterPlugin.class);

    @Override
    public void execute(WebExchange exchange, AuthSphereAfterPluginChain chain) {
        PluginData pluginData = BaseDataCache.getInstance().obtainPluginData(name());
        if (pluginData == null || !pluginData.getEnabled()) {
            chain.execute(exchange);
            return;
        }
        try {
            doExecute(exchange, chain, pluginData);
        } catch (Exception e) {
            log.error("{} plugin error", name(), e);
            exchange.setAbort(true);
        }
    }

    protected abstract void doExecute(WebExchange context, AuthSphereAfterPluginChain chain, PluginData pluginData);
}
